# -*- coding: utf-8 -*-
from scrapy.selector import Selector
import scrapy
from scrapy.contrib.loader import ItemLoader, Identity
from qunar.items import QunarItem

class QunarSpider(scrapy.Spider):
    name = "qunar"
    start_urls = (
        'http://travel.qunar.com/travelbook/list.htm?order=hot_heat',
    )
    
    def parse(self, response):
        sel = Selector(response)
        self.log("===========================| %s |" % sel)
        for link in  sel.css('li').xpath('@data-url').extract():
            links = 'http://travel.qunar.com' + link
            request = scrapy.Request(links, callback=self.parse_item)
            ## 将得到的页面地址传送给单个页面处理函数进行处理 -> parse_item
            yield request
        next_pages = sel.xpath('//div[@class="b_paging"]/a[@class="next"]/@href')
        if next_pages:
              next_page = next_pages[0].extract()
              self.log('page_url: %s' % next_page)
              yield scrapy.Request(next_page, callback=self.parse)
                   ## 将 「下一页」的链接传递给自身，并重新分析

    def parse_item(self, response):
            item = QunarItem()
            sel_item = Selector(response)
            items = []

            item['url'] = response
            item['place'] = sel_item.xpath('//p/a[2]/text()').extract()[0]
            item['name'] = sel_item.xpath('//p/span[3]/text()').extract()[0]
            item['image_urls'] = sel_item.xpath('//dl/dt/img/@data-original').extract()

            items.append(item)
            return items